package com.example.springweb.service;

import java.util.List;

import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.springweb.enums.SortEnum;
import com.example.springweb.model.Sort;
import com.example.springweb.model.dto.ProductDTO;
import com.example.springweb.model.entity.Product;
import com.example.springweb.model.vo.ProductVo;
import com.example.springweb.util.ConvertUtil;

import cn.hutool.core.util.StrUtil;

public interface IProductService {

	ProductVo getProductById(Long id);
	
	Product selectById(Long id);
	
	int insert(Product product);
	
	IPage<ProductVo> findPruductByCategory(ProductDTO dto);
	
	List<Product> selectList(ProductDTO product);
	
	default void orderBy(List<Sort> sorts, QueryWrapper<Product> wrapper) {
		if(!CollectionUtils.isEmpty(sorts)) {
			for(Sort st : sorts) {
				String fields = ConvertUtil.camelToSnake(st.getFields());
				boolean isAsc = SortEnum.ASC.name().equalsIgnoreCase(st.getDirection());
				if(StrUtil.isNotBlank(fields)) {
					wrapper.orderBy(true, isAsc, fields);
					
				}
			}
		}
	}
}
